﻿using ScreenMonitor.Common.File;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MyServer.Common;
using MyServer.TcpIP;
using System.Net.Sockets;

namespace ClientUI
{
    public partial class FileReciveWnd : Form
    {
        public FileReciveWnd()
        {
            InitializeComponent();
        }

        private class SendFileItem
        {
            public string Path;
            public TcpClient Client;
        }

        private delegate void AddNewRequestDel(FileRequestObject obj);
        public void AddNewRequest(FileRequestObject obj)
        {
            if (this.ListVReciveQueue.InvokeRequired)
            {
                AddNewRequestDel del = new AddNewRequestDel(AddNewRequest);
                this.ListVReciveQueue.BeginInvoke(del, obj);
            }
            else
            {
                ListViewItem item = new ListViewItem(((TcpClient)(obj.Tag)).Client.RemoteEndPoint.ToString());
                item.SubItems.Add("准备发送");
                item.SubItems[1].Tag = "未知";
                SendFileItem i = new SendFileItem();
                i.Client = (TcpClient)obj.Tag;
                i.Path = obj.FilePath;
                item.Tag = i;
                ListVReciveQueue.Items.Add(item);
            }
        }

        private void FileReciveWnd_Load(object sender, EventArgs e)
        {

        }

        private void FileReciveWnd_Shown(object sender, EventArgs e)
        {
            ScreenMonitor.Common.File.FilePool.AfterOperateRequest += FilePool_AfterOperateRequest;
        }

        void FilePool_AfterOperateRequest(ScreenMonitor.Common.File.FileRequestEventArgs e)
        {
            if (!e.IsCanceled && e.RequestObject.Tag != null)
            {
                if (ListVReciveQueue.InvokeRequired)
                {
                    FilePool.BeforeOperateRequestEventHandler del = new FilePool.BeforeOperateRequestEventHandler(FilePool_AfterOperateRequest);
                    ListVReciveQueue.BeginInvoke(del, e);
                }
                else
                {
                    foreach (ListViewItem item in ListVReciveQueue.Items)
                    {
                        SendFileItem i = (SendFileItem)item.Tag;
                        if (i.Client == e.RequestObject.Tag && i.Path == e.RequestObject.FilePath)
                        {
                            switch (e.RequestObject.ReauestType)
                            {
                                case FileRequestType.Close:
                                    item.SubItems[1].Text = "已完成";
                                    break;
                                case FileRequestType.Error:
                                    item.SubItems[1].Text = "远端出错：" + Encoding.UTF8.GetString(e.RequestObject.Buffer);
                                    break;
                                case FileRequestType.Open:
                                    item.SubItems[1].Text = "正在接受请求";
                                    item.SubItems[1].Tag = BitConverter.ToInt64(e.ResponceObject.Buffer, 0);
                                    break;
                                case FileRequestType.OpenOrCreate:
                                    item.SubItems[1].Text = "正在准备写入";
                                    item.SubItems[1].Tag = BitConverter.ToInt64(e.RequestObject.Buffer, 0);
                                    break;
                            }
                            switch (e.ResponceObject.ReauestType)
                            {
                                case FileRequestType.Close:
                                    item.SubItems[1].Text = "已完成";
                                    break;
                                case FileRequestType.Error:
                                    item.SubItems[1].Text = "本地出错：" + Encoding.UTF8.GetString(e.ResponceObject.Buffer);
                                    break;
                                case FileRequestType.Read:
                                    item.SubItems[1].Text = "正在传输读取：" + e.ResponceObject.FileOffset.ToString() + "/" + item.SubItems[1].Tag.ToString();
                                    break;
                                case FileRequestType.Write:
                                    item.SubItems[1].Text = "正在传输写入：" + e.ResponceObject.FileOffset.ToString() + "/" + item.SubItems[1].Tag.ToString();
                                    break;
                            }
                        }
                    }
                }
            }
        }
    
    }
}
